package org.glut.service;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

@Service
public class MessageService {

    @Autowired
    private CFAirService cfAirService;
    @Autowired
    private YHAirService yhAirService;
    @Autowired
    private HWJCService hwjcService;
    @Autowired
    private RFService rfService;

    @Autowired
    private GZService gzService;

    private String address;
    private int port;
    InputStream inputStream;
    OutputStream outputStream;
    public Socket socket;

    public void mesService(String address, int port) {
        this.port = port;
        this.address = address;
        byte[] buff = new byte[1024 * 4];
        try {
            socket = new Socket(address, port);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            int len = -1;
            while ((len = inputStream.read(buff)) != -1) {
                String s = new String(buff, 0, len, "gbk");
                System.out.println(s);

                String[] messageArray = s.split("E");
                for (String str : messageArray) {
                    String[] s1 = str.split(" ");
                    // System.out.println(s);
                    if ("CF2".equals(s1[0]) || "CF1".equals(s1[0])) {
                        if (cfAirService == null) {
                            System.out.println("cfAirService == null");
                        }
                        try {
                            cfAirService.getmsgs(s1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if ("YH2".equals(s1[0]) || "YH1".equals(s1[0])) {
                        if (yhAirService == null) {
                            System.out.println("yhAirService == null");
                        }
                        try {
                            yhAirService.getmsgs(s1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if ("HW2".equals(s1[0]) || "HW1".equals(s1[0])) {
                        if (hwjcService == null) {
                            System.out.println("hwjcService == null");
                        }
                        try {
                            hwjcService.getmsgs(s1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if ("RF2".equals(s1[0]) || "RF1".equals(s1[0])) {
                        if (rfService == null) {
                            System.out.println("rfjcService == null");
                        }
                        try {
                            rfService.getmsgs(s1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }else if ("GZ2".equals(s1[0]) || "GZ1".equals(s1[0])) {
                        if (gzService == null) {
                            System.out.println("gzjcService == null");
                        }
                        try {
                            gzService.getmsgs(s1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }


//                s =s.substring(0, s.indexOf("E"));
//                String[] s1 = s.split(" ");
//
//                if ("CF2".equals(s1[0])||"CF1".equals(s1[0])){
//                    cfAirService.getmsgs(s1);
//                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    public boolean sendCommand(String command) {
        if (outputStream != null) {
            try {
                outputStream.write(command.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }
        }
        return false;
    }

}
